package br.com.decorando.activity;

import java.util.Date;

import br.com.decorando.dao.CartaDAO;
import br.com.decorando.model.Carta;
import br.comdecorando.adapter.CartaAdapter;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RevisaPerguntasActivity extends Activity {
	CartaAdapter adapter;
	int idAssunto = 0;
	Carta carta;
	CartaDAO cartaDAO = new CartaDAO(this);
	TextView pergunta;
	TextView resposta;
	Button mostrar;
	Button errei;
	Button dificil;
	Button facil;
	Button medio;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_revisa_perguntas);
		pergunta = (TextView) findViewById(R.id.txvEnunciadoPergunta); 
		resposta = (TextView) findViewById(R.id.txvEnunciadoResposta);
		mostrar = (Button) findViewById(R.id.btnMostrar);
		errei = (Button) findViewById(R.id.btnErrei);
		dificil = (Button) findViewById(R.id.btnDificil);
		facil = (Button) findViewById(R.id.btnFacil);
		medio = (Button) findViewById(R.id.btnMedio);
		Intent intent = getIntent();
		idAssunto = intent.getIntExtra("idAssunto", 1);
		carta = cartaDAO.getCartaParaRevisar(idAssunto);
		avancarCarta();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.revisa_perguntas, menu);
		return true;
	}
	
	public void mostrar(View view) {
		mostrar.setVisibility(View.INVISIBLE);
		errei.setVisibility(View.VISIBLE);
		dificil.setVisibility(View.VISIBLE);
		facil.setVisibility(View.VISIBLE);
		medio.setVisibility(View.VISIBLE);
		resposta.setVisibility(View.VISIBLE);
	}
	
	public void avancarCarta() {
		pergunta.setText(carta.getPergunta());
		resposta.setText(carta.getResposta());
		resposta.setVisibility(View.INVISIBLE);
		mostrar.setVisibility(View.VISIBLE);
		errei.setVisibility(View.INVISIBLE);
		dificil.setVisibility(View.INVISIBLE);
		facil.setVisibility(View.INVISIBLE);
		medio.setVisibility(View.INVISIBLE);
	}
	
	public void facil(View view) {
		carta.setDificuldade(1);
		atualizarPassarProximaCarta();
	}
	public void medio(View view) {
		carta.setDificuldade(2);
		atualizarPassarProximaCarta();
	}
	public void dificil(View view) {
		carta.setDificuldade(3);
		atualizarPassarProximaCarta();
	}
	public void errei(View view) {
		carta.setDificuldade(4);
		atualizarPassarProximaCarta();
	}
	
	private void atualizarPassarProximaCarta() {
		carta.setDataUltimaRevisao(new Date());
		cartaDAO.update(carta);
		carta = cartaDAO.getCartaParaRevisar(idAssunto);
		avancarCarta();
	}
	
	public void finalizar(View view) {
    	this.finish();
	}

    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
    	switch (item.getItemId()) {
			case R.id.menu_perfil:
				intent = new Intent(this, PerfilActivity.class);
				startActivity(intent);
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
    }
}
